package com.chinaedu.oc.action.base;

import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.chinaedu.oc.constant.Constant;
import com.chinaedu.oc.util.common.StringUtil;


public class BaseController {
	@InitBinder
	public void InitBinder(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Timestamp.class, new PropertyEditorSupport() {
			public void setAsText(String value) {
				//System.out.println("date:" + value);
				if (StringUtil.isBlack(value)) {
					setValue(null);
				} else {
					try {
						java.util.Date d = new SimpleDateFormat(Constant.DATE_FORMAT).parse(value);
						setValue(new Timestamp(d.getTime()));
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
			}

//			public String getAsText() {
//				// return new
//				// SimpleDateFormat(Constant.DATE_FORMAT).format((Date)
//				// getValue());
//				return ((Timestamp) getValue()).getTime() + "";
//			}
		});
	}
}
